package com.example.notify_click;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends Activity {
    NotificationManager notificationManager;
    private IntentFilter intentFilte2;
    final String clickNotify = "clickNotify";
    NotificationClickReceiver notificationClickReceiver;
    private LocalBroadcastManager localBroadcastManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
/*        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
        builder1.setSmallIcon(R.mipmap.ic_launcher); //设置图标
        builder1.setTicker("显示第二个通知");
        builder1.setContentTitle("通知"); //设置标题
        builder1.setContentText("点击查看详细内容"); //消息内容
        builder1.setWhen(System.currentTimeMillis()); //发送时间
        builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音，振动方式，灯光
        builder1.setAutoCancel(true);//打开程序后图标消失
        Intent intent =new Intent (MainActivity.this,NotificationClickReceiver.class);
        PendingIntent pendingIntent =PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
        builder1.setContentIntent(pendingIntent);
        Notification notification1 = builder1.build();
        notificationManager.notify(124, notification1); // 通过通知管理器发送通知*/
        //点击通知栏
        intentFilte2 = new IntentFilter(clickNotify);
        notificationClickReceiver =  new NotificationClickReceiver();
        localBroadcastManager.registerReceiver(notificationClickReceiver, intentFilte2);

        Notification notification = android8();
        notificationManager.notify(100,notification);
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        channel.setSound(null,null);   /*gxw+ for 静音*/
        notificationManager.createNotificationChannel(channel);
    }
    public Notification android8()
    {
        String channelId;

        channelId = "com.anyikang.fallingalarm";
        String channelName = "Android O com.anyikang.fallingalarm";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        createNotificationChannel(channelId, channelName, importance);



        RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);
//        设置对应IMAGEVIEW的ID的资源图片
        view_custom.setImageViewResource(R.id.custom_icon,R.mipmap.ic_launcher);
        view_custom.setViewVisibility(R.id.battery, View.VISIBLE);


        Intent intent = new Intent (clickNotify);
        intent.setClass(MainActivity.this, NotificationClickReceiver.class);//静态注册的广播，必须显示的指定类。
        PendingIntent pendingIntent =PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);


        Notification notification = new Notification.Builder(this,channelId)
                .setContentTitle("click me")
                .setContentText("正在后台运行")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setTicker("FallAlarm正在后台为您监护")
                .setPriority(Notification.PRIORITY_DEFAULT)
                //gxw-试验取消铃声  .setDefaults(Notification.DEFAULT_ALL)
                .setOngoing(true)
                .setCustomContentView(view_custom)
                /*gxw+s for 静音*/
                .setOnlyAlertOnce(true)
                .setSound(null)
                .setVibrate(null)
                .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)//取消震动,铃声其他都不好使
                /*gxw+e for 静音*/
                .build();


        notification.flags=Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;


        //gxw-想关闭铃声不起作用 notification.defaults = Notification.DEFAULT_SOUND;
        /*gxw+s for 静音*/
        notification.defaults = 0;//GXW+ FOR JINGYIN
        notification.sound = null;
        notification.vibrate = null;
    //gxw-    notification.contentIntent = pendingIntent;
        /*gxw+e for 静音*/
        view_custom.setOnClickPendingIntent(R.id.notifyLayout, pendingIntent);

        return notification;
    }
}
